package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ArrayRemovesAllOfItsElementsThatEqualsWithElementParameter {
	public static <T> T[] removeElement(Class<T> typeClass, T[] array,
	                                    T element) {
		int objectCount = contains(typeClass, array, element);
		if (objectCount > 0) {
			@SuppressWarnings("unchecked")
			T[] newArray = (T[]) Array.newInstance(typeClass, array.length
					- objectCount);
			int j = 0;
			for (T t : array) {
				if (!t.equals(element)) {
					newArray[j] = t;
					j++;
				}
			}

			return newArray;
		}
		return array;
	}

	public static <T> int contains(Class<T> typeClass, T[] array, T element) {
		int counter = 0;
		for (T t : array) {
			if (t.equals(element)) {
				counter++;
			}
		}
		return counter;
	}
}
